package  
{
	import flash.display.Graphics;
	import flash.geom.Point;
	import samples.BaseSample;
	import systems.spidron.Spidron;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class SpidronSample extends BaseSample 
	{
		private var iterations:int;
		
		public function SpidronSample() 
		{
			
			var size:Number = 150;
			
			
			iterations = 10;
			var i:Number;
			
			var multi:Number = Math.sin( Math.PI / 3 );
			
			for ( i = 0; i < Math.PI * 2; i += Math.PI / 3 )
			{
				
				
				var sp:Spidron = new Spidron( sw / 2, sh / 2, size, 0, 35 / 180 * Math.PI );
				
				sp.reset();

				sp.x = sw / 2 + Math.cos( i ) * ( size * multi );
				sp.y = sh / 2 + Math.sin( i ) * ( size * multi );
				sp.angle = i + Math.PI / 2;
				sp.build( graphics, iterations );
				draw( sp );
				
				
				
				sp.reset();
				
				sp.x = sw / 2 + Math.cos( i ) * ( size * multi );
				sp.y = sh / 2 + Math.sin( i ) * ( size * multi );
				sp.angle = i + Math.PI / 2 + Math.PI;
				
				
				sp.build( graphics, iterations );
				draw( sp );
				graphics.lineStyle( 2, 0xFF00 );
				drawTri( sp.points[ 0 ], sp.points[ 1], sp.points[ sp.points.length - 3 ], graphics );
				
				
				
			}
			
		}
		
		private function draw( spidron:Spidron ):void
		{
			var i:int;
			var a:Point, b:Point, c:Point, d:Point;
			
			for ( i = 0; i < spidron.points.length; i+=4 )
			{
				a = spidron.points[ i ];
				b = spidron.points[ i + 1 ];
				c = spidron.points[ i + 2 ];
				d = spidron.points[ i + 3 ];
				
				graphics.lineStyle( 0, 0x333333 ); 
				
				drawTri( a, b, c, graphics );
				drawTri( a, c, d, graphics );
				
				graphics.lineStyle( 0, 0xDDDDDD ); 
				
				drawQuad( a, b, c, d, graphics );
				
			}
		}
		
		private function drawTri(a:Point, b:Point, c:Point, graphics:Graphics):void 
		{
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.lineTo( a.x, a.y );
		}
		
		private function drawQuad( a:Point, b:Point, c:Point, d:Point, graphics:Graphics):void 
		{
			graphics.moveTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( d.x, d.y );
			
		}
	}

}